package com.jd.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jd.domain.User;
import com.jd.feign.UserService;
import com.jd.sentinel.getOrderFallback;
import com.jd.sentinel.getOrderblockHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserService userService;

    @GetMapping("/getOrderById/{id}")
    @SentinelResource(value = "order-getOrderById",
                    blockHandler = "OrderblockHandler",
                    blockHandlerClass = getOrderblockHandler.class,
                    fallback = "OrderFallback",
                    fallbackClass = getOrderFallback.class)
    public User getOrderById(@PathVariable("id") Long id) {

        // int i = 1/0;
        // 调用OpenFeign接口
        User userById = userService.getUserById(id);
        System.out.println(userById);
        return new User(1L,"看什么哇","小骚逼");
       // return userService.getUserById(id);
    }

//    // 限流 本类
//    public User OrderblockHandler(@PathVariable("id") Long id, BlockException e){
//        return new User(-1L,"限流啦","点点爆了！");
//    }
//
//
//    // 必须有一个参数Throwable（熔断）本类
//    public User OrderFallback(@PathVariable("id") Long id,Throwable e){
//        return new User(-1L,"降级了","遭不住了");
//    }


}
